<?php
require_once( '../../../libs/smarty.inc' );
require_once( '../../../libs/Class_DB.php' );
require_once( '../../../libs/Class_ERROR.php' );
require_once( '../../../libs/login.inc.php' );
require_once( '../../../libs/const.php' );

if( !in_array( "A", $_SESSION['ADMIN_LOGIN']['AUTH_CD'] ) ) exit;

$db = new Class_DB;

// 製品・単価を読込み
$db->query( "SELECT * FROM M_PRODUCTS WHERE category <> 5" );
$products[''] = '';
while( $row = $db->fetch() ){
	$products[ $row['id'] ] = $row['pro_name'];
	$costs[ $row['id'] ] = $row['cost'];
}


if( $_SERVER['REQUEST_METHOD'] == 'GET' ){
	
	if( isset($_GET['id']) && $_GET['id'] != '' ){
		
		if( is_numeric( $_GET['id'] ) ){
			
			$sql = sprintf("SELECT * FROM D_SAMPLE WHERE id = %u", $_GET['id'] );
			$res = $db->query( $sql );
			$forms = mysql_fetch_array( $res, MYSQL_ASSOC );
			
			$sql = sprintf("SELECT * FROM D_SAMPLE_DETAIL WHERE id = %u ORDER BY num", $_GET['id'] );
			$db->query( $sql );
			while( $row = $db->fetch() ){
				$forms['category'][$row['num']]	= $row['category'];
				$forms['products'][$row['num']]	= $row['pro_cd'];
				$forms['amount'][$row['num']]	= $row['amount'];
				$forms['model'][$row['num']]	= $row['model'];
				$forms['size'][$row['num']]		= $row['size'];
				$forms['unit'][$row['num']]		= $row['unit'];
				$forms['biko'][$row['num']]		= $row['biko'];
				$forms['cost'][$row['num']]		= $row['cost'];
			}

		}

	}
	
}

$total = 0;
foreach( $forms['products'] as $k => $v ){
	if( $forms['amount'][$k] != '' && is_numeric( $forms['amount'][$k] ) ){
		$tmp_subt = $forms['cost'][$k] * $forms['amount'][$k];
		$total += $tmp_subt;
		$subtotal[$k] = '&yen;' . number_format($tmp_subt);
	}
}
$total = '&yen;' . number_format($total);

$forms['r_date'] = str_replace('-', '/', substr($forms['r_date'], 0, 10) );

//if( isset( $forms['id'] ) && $forms['id'] != '' ) $forms['disp_id'] = 'B' . sprintf( '%05d',$forms['id'] );
$forms['disp_id'] = 'A' . sprintf( '%05d',$forms['id'] );

$smarty->assign('products', $products);
$smarty->assign('costs', $costs);
$smarty->assign('total', $total);
$smarty->assign('specify', array( 1=> 'あり', 0=> 'なし'));
$smarty->assign('category', $category );
$smarty->assign('arr_denpyo', $arr_denpyo );

if( isset( $subtotal ) )$smarty->assign('subtotal', $subtotal);
if( isset( $forms ) )	$smarty->assign('forms', $forms);

$smarty->display('web-admin/preview/sample.html');


?>